package com.example.wanbaqian.utils;

import android.content.Context;
import android.util.Log;

import com.example.wanbaqian.base.BaseApplication;
import com.google.gson.Gson;
import com.ishumei.smantifraud.SmAntiFraud;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/4/15 0015.
 */

public class GetJsonUtils {
    public static GetJsonUtils utils = null;
    private static final String TAG = "GetJsonUtils";

    public static GetJsonUtils getInstance() {
        if (null == utils) {
            synchronized (GetJsonUtils.class) {
                if (null == utils) {
                    utils = new GetJsonUtils();
                }
            }
        }
        return utils;
    }

    /**
     * 得到请求的数据
     *
     * @param action 调用的接口
     * @param context
     * @param key
     * @param value
     * @return
     */
    public String getRequest(String action, Context context, String[] key, Object[] value) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> parentMap = new HashMap<>();
        map.put("common", getCommonRequest(action, context));
        map.put("content", getContentRequest(key, value));
        parentMap.put("request", map);
        String request = new Gson().toJson(parentMap);
        Log.e(TAG, "getRequest: " + request);
        return request;
    }

    /**
     * 得到公用的请求json
     *
     * @param action 调用的接口
     * @param context
     * @return
     */
    public Map getCommonRequest(String action, Context context) {
        Map<String, String> sonMap = new HashMap<>();
        sonMap.put("action",action);
        sonMap.put("reqtime", TimeUtils.getCurrentTimeByFormat());
        sonMap.put("ip", "");
        sonMap.put("trackid", "");
        sonMap.put("shumei_id", BaseApplication.deviceId);
        Log.i(TAG, "getCommonRequest: "+BaseApplication.deviceId);
        sonMap.put("version", BaseUtils.geVersionCode(context));
        sonMap.put("device_port", "android");
        return sonMap;
    }

    /**
     * 得到请求体的json数据
     *
     * @param key   jsonObject的key
     * @param value jsonObject的value  如果要请求jsonObject里面嵌套jsonObject的value只需要传map 否就传简单的数据类型
     * @return
     */
    public Map getContentRequest(String[] key, Object[] value) {
        Map<String, Object> map = new HashMap<>();
        for (int i = 0; i < key.length; i++) {
            if (value[i] instanceof Map) {
                Map<Object, Object> sonMap = new HashMap<>();
                Map<Object, Object> sonDateMap = (Map<Object, Object>) value[i];
                List<String> keyList = getMapKey(sonDateMap);
                for (int j = 0; j < keyList.size(); j++) {
                    sonMap.put(keyList.get(j), sonDateMap.get(keyList.get(j)));
                }
                map.put(key[i], sonMap);
            } else {
                map.put(key[i], value[i].toString());
            }
        }
        return map;
    }

    /**
     * 获取map里面的所有值
     *
     * @param hashmp
     * @return
     */
    public List<String> getMapKey(Map hashmp) {
        List<String> list = new ArrayList<>();
        Set set = hashmp.keySet();
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            String key = (String) iter.next();
            list.add(key);
        }
        return list;
    }
}
